package com.cos.lession54.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/3/9 9:22
 * @description:
 */
@Controller
@RequestMapping("/demo2")
public class Demo2Controller {

    @RequestMapping("/d1")
    public String demo1(HttpServletRequest request, HttpSession session){
        request.setAttribute("username","张三");
        session.setAttribute("password", "123456");
        return "demo";
    }
    @RequestMapping("/d2")
    public String demo2(Model model){
        //相当于 request.setAttribute("username","admin");
        // ModelAndView
        //Model model 也就是把值存入request作用域中
        model.addAttribute("username", "admin");
        return "demo";
    }

    @RequestMapping("/d3")
    public ModelAndView demo3(){
        //直接返回ModelAndView
        //创建ModelAndView
        ModelAndView mv= new ModelAndView();
        //设置参数到request作用域
        //model
        mv.addObject("username","李四");
        //设置逻辑视图名
        //view
        mv.setViewName("demo");
        return mv;
    }

    @RequestMapping("/d4")
    public String demo4(Map<String,Object> datas){
        //直接使用map集合默认传值,而不是接受参数,如果要使用map集合接受参数,就要加上@RequestParam注解
        //直接使用map集合,他默认就会被作为参数封装到modelAndView 的model数据中
        //默认requests作用域
        datas.put("username","王五");
        datas.put("password", "3333");
        return "demo";
//        return "redirect:/demo.jsp";
    }


}
